package com.bnz.controller;

import com.bnz.pojo.Classes;
import com.bnz.pojo.Course;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.CourseService;
import com.bnz.service.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    // 1. 查询所有学生
    @RequestMapping("/list")
    public String list(Model model){
        // 1.1 查询所有学生
        List<Student> students = studentService.findAll();
        // 1.2 将查询到的信息放到model中
        model.addAttribute("student",students);
        // 1.3 返回视图
        return ("student/list");
    }
    // 2. 分页查询
    @RequestMapping("findByPage")
    public String findByPage(@RequestParam(required = false, defaultValue = "1") int page,
                             @RequestParam(required = false, defaultValue = "5") int pageSize ,
                             Model model){
        // 2.1 查询所有学生
        PageResult<Student> pageResult = studentService.findByPage(page,pageSize);
        // 2.2 查询所有课程
        List<Course> courses = courseService.findAll();
        // 2.3 将查询到的数据放到model中
        model.addAttribute("pr",pageResult);
        model.addAttribute("course",courses);
        //2.4 返回视图
        return ("student/list");
    }
    // 3. 条件查询带分页
    @RequestMapping("/search")
    public String search(@RequestParam(required = false, defaultValue = "1") int page,
                         @RequestParam(required = false, defaultValue = "5") int pageSize,
                         Student student,
                         Model model){
        // 3.1 查询所有学生
        PageResult<Student> pr = studentService.search(page,pageSize,student);
        // 3.2 查询所有课程
        List<Course> courses = courseService.findAll();
        // 3.3 将查询到的数据放到model中
        model.addAttribute("pr",pr);
        model.addAttribute("course",courses);
        //model.addAttribute("student",student);
        // 3.4 返回视图
        return ("student/list");
    }

    // 4. 从查询页面跳转到学生信息添加修改页面
    @RequestMapping("/editPage")
    public String editPage(Model model,String sid){
        // 4.1 根据sid查找学生
        Student student = studentService.findById(sid);
        // 4.2 查询所有课程
        List<Course> courses = courseService.findAll();
        // 4.3 将查询到的信息放到model中
        model.addAttribute("student",student);
        model.addAttribute("course",courses);
        // 4.4 返回到添加，修改学生页面
        return ("student/edit");
    }

    // 5. 添加学生
    @RequestMapping("/add")
    public String add(Student student){
        // 5.1 添加学生
        studentService.add(student);
        // 5.2 返回搜索学生页面
        return "redirect:/student/search.do";
    }

    // 6. 修改学生
    @RequestMapping("/update")
    public String update(Student student){
        // 6.1 修改学生
        studentService.update(student);
        // 6.2 返回搜索学生页面
        return "redirect:/student/search.do";
    }
    // 7. 删除学生
    @RequestMapping("/delete")
    public String delete(String sid){
        // 7.1 删除学生
        studentService.delete(sid);
        // 7.2 返回删除学生页面
        return "redirect:/student/search.do";
    }
}